package com.xs.bookspoststation.BookActivity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xs.bookspoststation.R;
import com.xs.bookspoststation.UserActivity.AddUserActivity;
import com.xs.bookspoststation.db.DBHelperBook;

public class AddBookActivity extends AppCompatActivity {
    //定义对象
    private EditText bookid,bookname,booknumber,bookauthor,bookshow;
    private Button submit,quit;
    //定义一个数据库帮助类
    private DBHelperBook dbHelperBook;
    //定义一个数据库操作类
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_book);
        //绑定控件
        bookid = findViewById(R.id.edit_add_bookid1);
        bookname = findViewById(R.id.edit_add_bookname1);
        booknumber = findViewById(R.id.edit_add_booknumber1);
        bookauthor = findViewById(R.id.edit_add_bookauthor1);
        bookshow = findViewById(R.id.edit_add_bookshow1);
        submit = findViewById(R.id.btn_add_book2);
        quit = findViewById(R.id.btn_add_cancel2);
        //添加按钮点击事件
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取用户输入的数据
                String tempBookId = bookid.getText().toString();
                String tempBookName = bookname.getText().toString();
                String str_tempBookNumber = booknumber.getText().toString();
                String tempBookauthor = bookauthor.getText().toString();
                String tempBookshow = bookshow.getText().toString();
                //将图书数量转换为int类型
                int tempBookNumber =Integer.parseInt(str_tempBookNumber);

                boolean AddBook = true;
                //读取数据库
                dbHelperBook = new DBHelperBook(AddBookActivity.this,"book.db",null,1);
                db = dbHelperBook.getWritableDatabase();
                //判断信息是否填写完整
                if (tempBookId.equals("") || tempBookName.equals("") || tempBookauthor.equals("") || tempBookshow.equals("") || str_tempBookNumber.equals("")){
                    Toast.makeText(AddBookActivity.this, "信息未填写完整！！！", Toast.LENGTH_SHORT).show();
                }else {
                    Cursor cursor = db.query("bookInfo",new String[]{"bookid"},null,null,null,null,null);
                    while (cursor.moveToNext()){
                        if (tempBookId.equals(cursor.getString(cursor.getColumnIndex("bookid")))){
                            Toast.makeText(AddBookActivity.this, "图书编号已存在!!!", Toast.LENGTH_SHORT).show();
                            AddBook = false;
                            break;
                        }
                    }
                    if (AddBook){
                        //存储类，只能为String类型
                        ContentValues values = new ContentValues();
                        values.put("bookid",tempBookId);
                        values.put("bookname",tempBookName);
                        values.put("booknumber",str_tempBookNumber);
                        values.put("bookauthor",tempBookauthor);
                        values.put("bookshow",tempBookshow);
                        db.insert("bookInfo",null,values);
                        db.close();//关闭数据库
                        Log.v("增加图书信息", "增加图书信息成功");
                        AlertDialog alertDialog = new AlertDialog.Builder(AddBookActivity.this).create();
                        alertDialog.setTitle("添加成功");
                        alertDialog.setMessage("是否查看？");
                        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        });
                        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        alertDialog.show();
                    }
                }
            }
        });
        //返回按钮点击事件
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(AddBookActivity.this,OperateBookActivity.class);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "返回管理图书界面", Toast.LENGTH_LONG).show();
            }
        });

    }
}
